package com.sync.syncmasterdata;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import com.sync.syncmasterdata.customer.controller.CusSyncController;
import com.sync.syncmasterdata.dingrobot.controller.DingController;
import com.sync.syncmasterdata.dingrobot.pojo.DingMsg;
import com.sync.syncmasterdata.product.entity.wms.EleProduct;
import com.sync.syncmasterdata.product.pojo.cp.InventoryForCP;
import com.sync.syncmasterdata.product.pojo.erp.InventoryForU8;
import com.sync.syncmasterdata.product.pojo.oa.InventoryForOA;
import com.sync.syncmasterdata.product.service.SyncInventoryService;
import com.sync.syncmasterdata.product.service.convert.ProductOAtoCP;
import com.sync.syncmasterdata.product.service.convert.ProductOAtoWms;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@SpringBootTest
@Slf4j
class SyncMasterDataApplicationTests3 {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private DingController dingController;

    @Autowired
    private SyncInventoryService syncInventoryService;

    @Autowired
    private ProductOAtoWms productOAtoWms;

    @Autowired
    private ProductOAtoCP productOAtoCP;

    @Autowired
    private CusSyncController cusSyncController;

    @Test
    void contextLoads() {
    }

    /**
     * 钉钉推送测试
     */
    @Test
    public void interfaceTestDing() {
        DingMsg dingMsg = new DingMsg();
        dingMsg.setMsg("https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg");
        dingMsg.setPhones(Arrays.asList("15268111548"));
        dingController.pushSimpleImageByDing(dingMsg);
    }

    @Test
    public void interfaceTestDing2() {
        Map<String, Object> dingMsg = new HashMap<>();
        dingMsg.put("title","这是一个标题");
        dingMsg.put("text","这个是内容");
        dingMsg.put("picUrl","https://img.alicdn.com/imgextra/i1/O1CN0122tBjy1UKaVbrgShF_!!6000000002499-2-tps-2868-862.png");
        dingMsg.put("messageUrl","https://www.baidu.com");
        dingMsg.put("phones",Arrays.asList("15268111548"));
        dingController.pushSimpleLinkByDing(dingMsg);
    }

    @Test
    public void pojoTest() {
        InventoryForCP cp = new InventoryForCP();
    }

    /**
     * 测试cp新增接口
     */
    @Test
    public void interfaceTest5() {
        String requestid = "384885";
        List<InventoryForOA> lists = syncInventoryService.getWorkflowInfo(requestid);

        for (InventoryForOA oa : lists) {

            String code = this.syncInventoryService.getLastCode(oa.getChanpinlx());
            oa.setCunhuobm(code);

            InventoryForCP cp = this.productOAtoCP.convertTo(oa);
            log.info("ID展示:erp-{}||crm-{}||wms-{}", code);
            cp.setCrmid("crmid");
            cp.setErpid(oa.getCunhuobm());
            cp.setWmsid("wmsid");
            cp.setUserid("");
            String oid = this.syncInventoryService.addInvToCP(cp);
            System.out.println(oid);
        }

    }

    @Test
    public void interfaceTest6(){
        List<Integer> l = new ArrayList<>();
        l.add(52);
        l.add(53);
        for (Integer i : l) {
            syncInventoryService.updateProductResult(i,null);

        }
    }

    @Test
    public void interfaceFunction() {
        //多温度 等待前端完成后处理
        String[] names = splitStr("test1,test2,test3");
        String[] wenducs = splitStr("1,-2,");
        String[] wenduzs = splitStr(",,-8");
        //数组长度校验
        if (names.length != wenducs.length || wenduzs.length != names.length || wenducs.length != wenduzs.length) {

            System.out.println("未通过");
        } else {
            //校验通过
            System.out.println("通过");
            EleProduct[] eleProducts = new EleProduct[names.length];
            for (int i = 0; i < names.length; i++) {
                EleProduct eleProduct = new EleProduct();
                eleProduct.setSName(names[i]);
                eleProduct.setDTemperatureFrm(strToDouble(wenducs[i]));
                eleProduct.setDTemperatureTo(strToDouble(wenduzs[i]));
                eleProducts[i] = eleProduct;
            }
        }
    }


    /**
     * split 功能封装
     */
    private String[] splitStr(String s) {
        try {
            if (StringUtils.isBlank(s)) {
                return null;
            }
            String[] str = s.split("\\,",-1);
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    private Double strToDouble(String s) {
        try {
            if (StringUtils.isBlank(s)) {
                return null;
            }else{
                return Double.parseDouble(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
